<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "user".
 *
 * @property int $id
 * @property string $old_id 用户id（用户表）
 * @property string $name 姓名
 * @property string $phone 手机号
 * @property string $idcard 身份证
 * @property string $bankCard 银行卡号
 * @property string $city 城市
 * @property string $address 街道地址
 * @property string $create_time 创建时间
 * @property int $zm_score 信用分
 * @property string $openid openid
 * @property string $code 自己的推荐码
 * @property string $yq_code 别人的推荐码
 * @property string $biz_no
 * @property string $order_info
 * @property string $last_time 上次修改时间
 * @property string $comefrom H5_ZFB，XCX_ZFB，H5_JD
 * @property string $xch_sqxx 小程序授权信息
 * @property string $zm_user_id 芝麻信用用户ID
 * @property string $eme_contact 紧急联系人
 * @property string $eme_contact_phone 紧急联系人电话
 */
class Users extends \yii\db\ActiveRecord
{

    const SCENARIO_LOGIN = 'login';
    const SCENARIO_REGISTER = 'register';

    //验证场景 覆盖父类的方法  系统默认场景是default,
    //默认场景会验证模型的所有字段
    public function scenarios()
    {
        //获取父类的场景方法，默认场景
        $scenarios = parent::scenarios();
        //添加新的场景，只验证某些字段，其余字段不作验证
        $scenarios[self::SCENARIO_LOGIN] = ['name', 'phone'];
        $scenarios[self::SCENARIO_REGISTER] = ['name', 'phone', 'idcard'];
        return $scenarios;

    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
                // if you're using datetime instead of UNIX timestamp:
                // 'value' => new Expression('NOW()'),
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user';
    }



    /** 模型字段的验证规则
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['create_time'], 'safe'],
            [['zm_score'], 'integer'],
            [['order_info', 'xch_sqxx'], 'string'],
            [['old_id', 'bankCard', 'comefrom', 'zm_user_id', 'eme_contact'], 'string', 'max' => 30],
            [['name', 'city', 'address'], 'string', 'max' => 64],
            [['phone', 'eme_contact_phone'], 'string', 'max' => 11],
            [['idcard', 'last_time'], 'string', 'max' => 20],
            [['openid'], 'string', 'max' => 50],
            [['code', 'yq_code'], 'string', 'max' => 15],
            [['biz_no'], 'string', 'max' => 40],
            [['phone'], 'unique'],
        ];
    }

    /** 熟悉标签
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'old_id' => 'Old ID',
            'name' => 'Name',
            'phone' => 'Phone',
            'idcard' => 'Idcard',
            'bankCard' => 'Bank Card',
            'city' => 'City',
            'address' => 'Address',
            'create_time' => 'Create Time',
            'zm_score' => 'Zm Score',
            'openid' => 'Openid',
            'code' => 'Code',
            'yq_code' => 'Yq Code',
            'biz_no' => 'Biz No',
            'order_info' => 'Order Info',
            'last_time' => 'Last Time',
            'comefrom' => 'Comefrom',
            'xch_sqxx' => 'Xch Sqxx',
            'zm_user_id' => 'Zm User ID',
            'eme_contact' => 'Eme Contact',
            'eme_contact_phone' => 'Eme Contact Phone',
        ];
    }



    //获取订单
    public function getOrders(){

        //主表的字段当键值，从表字段做键
        return $this->hasMany(Orders::class,['user_phone'=>'phone']);
    }
}
